package com.zrrd.jt_admin.manager.controller;


import com.zrrd.jt_admin.common.entity.Rule;
import com.zrrd.jt_admin.common.response.ResponseResult;
import com.zrrd.jt_admin.common.page.RulePageVo;
import com.zrrd.jt_admin.manager.service.RuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanjl
 * @since 2024-06-25
 */
@Api(tags = "权限管理")
@RestController
@RequestMapping("/admin/rule")
public class RuleController {
    
    @Resource
    private RuleService ruleService;

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/{page}")
    @ResponseBody
    public ResponseResult findByPage(@PathVariable("page") int page) {
        RulePageVo rulePageVo=ruleService.findByPage(page);
        return ResponseResult.queryResponse(rulePageVo);
    }

    @ApiOperation(value = "添加", notes = "添加权限信息")
    @PostMapping
    @ResponseBody
    public ResponseResult add(@RequestBody Rule rule) {
        return ResponseResult.addResponse(ruleService.save(rule));
    }

    @ApiOperation(value = "修改", notes = "修改权限信息")
    @PutMapping
    @ResponseBody
    public ResponseResult update(@RequestBody Rule rule) {
        return ResponseResult.updateResponse(ruleService.updateById(rule));
    }

    @ApiOperation(value = "修改状态", notes = "根据id修改状态")
    @PutMapping("/{id}/updateStatus")
    @ResponseBody
    public ResponseResult setStatus(@PathVariable("id") Integer id,Integer status) {
        Boolean bStatus=status==1?true:false;

        boolean flag = ruleService.lambdaUpdate()//使用lambda形式的修改
                .eq(Rule::getId, id) //比较id
                .set(Rule::getStatus, bStatus)//进行修改
                .update();//执行修改
        return ResponseResult.updateResponse(flag);
    }

    @ApiOperation(value = "删除", notes = "根据id删除")
    @DeleteMapping("/{id}/delete")
    @ResponseBody
    public ResponseResult remove(@PathVariable("id") Integer id) {
        return ResponseResult.removeResponse(ruleService.removeById(id));
    }



}

